package com.yuyou.fn.controller;

import com.github.wens.mybatisplus.plugins.Page;
import com.yuyou.fn.common.constant.Constants;
import com.yuyou.fn.educational.entity.Campus;
import com.yuyou.fn.educational.entity.Room;
import com.yuyou.fn.educational.entity.Teacher;
import com.yuyou.fn.educational.service.ICampusService;
import com.yuyou.fn.educational.service.IRoomService;
import com.yuyou.fn.educational.service.ITeacherService;
import com.yuyou.fn.educational.vo.*;
import com.yuyou.fn.platform.entity.*;
import com.yuyou.fn.platform.service.*;
import com.yuyou.fn.platform.vo.SysResourceVo;
import com.yuyou.fn.platform.vo.SysUserQueryVo;
import com.yuyou.fn.platform.vo.SysUserSaveOrUpdateVo;
import io.swagger.annotations.ApiOperation;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.Collections;
import java.util.List;

/**
 * Created by wens on 16-11-29.
 */
@Controller
@RequestMapping("/api/admin/teaching/resource")
public class AdminTeachingResourceApiController extends AdminApiBaseController {

    @Resource
    private ITeacherService teacherService ;

    @Resource
    private ICampusService campusService ;

    @Resource
    private IRoomService roomService ;


    @ApiOperation("老师列表")
    @RequestMapping(value = "/teacherList" ,method = { RequestMethod.GET ,RequestMethod.POST })
    @ResponseBody
    public ReturnResult<Page<Teacher>> teacherList(TeacherQueryVo teacherQueryVo , @RequestParam(value = "pageNo" ,defaultValue = "1") int pageNo, @RequestParam(value = "pageSize" ,defaultValue = "20") int pageSize, HttpServletRequest request ) {
        SysUser user = getUser(request);
        if(user.getUserType() == Constants.USER_TYPE_OF_AREA ){
            teacherQueryVo.setAreaTeamId(getUser(request).getAreaTeamId());
        }else if(user.getUserType() == Constants.USER_TYPE_OF_BUS ){
            teacherQueryVo.setBusTeamId(user.getBusTeamId());
        }
        Page<Teacher> page = teacherService.findTeacherByQueryForPage(teacherQueryVo, pageNo, pageSize);
        return ReturnResult.success(page);
    }

    @ApiOperation("老师详情")
    @RequestMapping(value = "/teacherDetail" ,method = { RequestMethod.GET ,RequestMethod.POST })
    @ResponseBody
    public ReturnResult<Teacher> teacherDetail(@RequestParam("teacherId") Long teacherId) {
        Teacher teacher = teacherService.findById(teacherId);
        return ReturnResult.success(teacher);
    }

    @ApiOperation("保存更新老师")
    @RequestMapping(value = "/saveOrUpdateTeacher" ,method = { RequestMethod.GET ,RequestMethod.POST })
    @ResponseBody
    public ReturnResult<Void> saveOrUpdateTeacher(TeacherSaveOrUpdateVo teacherSaveOrUpdateVo ) {
        teacherService.saveOrUpdateTeacher(teacherSaveOrUpdateVo);
        return ReturnResult.success("Ok");
    }

    @ApiOperation("删除老师")
    @RequestMapping(value = "/delTeacher" ,method = { RequestMethod.GET ,RequestMethod.POST })
    @ResponseBody
    public ReturnResult<Void> delTeacher(@RequestParam("teacherId") Long teacherId ) {
        teacherService.deleteTeacher(teacherId);
        return ReturnResult.success("Ok");
    }


    @ApiOperation("校区列表")
    @RequestMapping(value = "/campusList" ,method = { RequestMethod.GET ,RequestMethod.POST })
    @ResponseBody
    public ReturnResult<Page<Campus>> campusList(CampusQueryVo campusQueryVo , @RequestParam(value = "pageNo" ,defaultValue = "1") int pageNo, @RequestParam(value = "pageSize" ,defaultValue = "20") int pageSize, HttpServletRequest request ) {
        SysUser user = getUser(request);
        if(user.getUserType() == Constants.USER_TYPE_OF_AREA ){
            campusQueryVo.setAreaTeamId(getUser(request).getAreaTeamId());
        }else if(user.getUserType() == Constants.USER_TYPE_OF_BUS ){
            campusQueryVo.setBusTeamId(user.getBusTeamId());
        }
        Page<Campus> page = campusService.findCampusByQueryForPage(campusQueryVo, pageNo, pageSize);
        return ReturnResult.success(page);
    }

    @ApiOperation("校区详情")
    @RequestMapping(value = "/campusDetail" ,method = { RequestMethod.GET ,RequestMethod.POST })
    @ResponseBody
    public ReturnResult<Teacher> campusDetail(@RequestParam("campusId") Long campusId) {
        Campus campus = campusService.findById(campusId);
        return ReturnResult.success(campus);
    }

    @ApiOperation("保存更新校区")
    @RequestMapping(value = "/saveOrUpdateCampus" ,method = { RequestMethod.GET ,RequestMethod.POST })
    @ResponseBody
    public ReturnResult<Void> saveOrUpdateCampus(CampusSaveOrUpdateVo campusSaveOrUpdateVo ) {
        campusService.saveOrUpdateCampus(campusSaveOrUpdateVo);
        return ReturnResult.success("Ok");
    }

    @ApiOperation("删除校区")
    @RequestMapping(value = "/delCampus" ,method = { RequestMethod.GET ,RequestMethod.POST })
    @ResponseBody
    public ReturnResult<Void> delCampus(@RequestParam("campusId") Long campusId ) {
        campusService.deleteCampus(campusId);
        return ReturnResult.success("Ok");
    }


    @ApiOperation("教室列表")
    @RequestMapping(value = "/roomList" ,method = { RequestMethod.GET ,RequestMethod.POST })
    @ResponseBody
    public ReturnResult<Page<Room>> roomList(RoomQueryVo roomQueryVo , @RequestParam(value = "pageNo" ,defaultValue = "1") int pageNo, @RequestParam(value = "pageSize" ,defaultValue = "20") int pageSize, HttpServletRequest request ) {

        if(roomQueryVo.getCampusId() == null ){
            throw new IllegalArgumentException("Invalid campusId") ;
        }

        Page<Room> page = roomService.findRoomByQueryForPage(roomQueryVo, pageNo, pageSize);
        return ReturnResult.success(page);
    }

    @ApiOperation("教室详情")
    @RequestMapping(value = "/roomDetail" ,method = { RequestMethod.GET ,RequestMethod.POST })
    @ResponseBody
    public ReturnResult<Room> roomDetail(@RequestParam("roomId") Long roomId) {
        Room room = roomService.findById(roomId);
        return ReturnResult.success(room);
    }

    @ApiOperation("保存更新教室")
    @RequestMapping(value = "/saveOrUpdateRoom" ,method = { RequestMethod.GET ,RequestMethod.POST })
    @ResponseBody
    public ReturnResult<Void> saveOrUpdateRoom(RoomSaveOrUpdateVo roomSaveOrUpdateVo ) {
        roomService.saveOrUpdateRoom(roomSaveOrUpdateVo);
        return ReturnResult.success("Ok");
    }

    @ApiOperation("删除教室")
    @RequestMapping(value = "/delRoom" ,method = { RequestMethod.GET ,RequestMethod.POST })
    @ResponseBody
    public ReturnResult<Void> delRoom(@RequestParam("roomId") Long roomId ) {
        roomService.deleteRoom(roomId);
        return ReturnResult.success("Ok");
    }

    @ApiOperation("所有区域校区列表")
    @RequestMapping(value = "/allCampus" ,method = { RequestMethod.GET ,RequestMethod.POST })
    @ResponseBody
    public ReturnResult<List<Campus>> allCampus( HttpServletRequest request ) {
        CampusQueryVo campusQueryVo = new CampusQueryVo();
        Page<Campus> page = campusService.findCampusByQueryForPage( campusQueryVo , 0, 5000);
        return ReturnResult.success(page.getRecords());
    }






}
